home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / string.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  4KB  |  158 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29.  
  30. #include "std.h"
  31. #include "string.h"
  32. #include "nsguid.h"
  33.  
  34. String::String(const char *initial_val) {
  35.   val = NULL;
  36.   setValue(initial_val);
  37. }
  38.  
  39. String::String(const String &s) {
  40.   val = NULL;
  41.   setValue(s.getValue());
  42. }
  43.  
  44. String::~String() {
  45.   FREE(val);
  46. }
  47.  
  48. const char *String::setValue(const char *newval) {
  49.   if (newval == val) return val; //FG
  50.   FREE(val); return val = STRDUP(newval);
  51. }
  52.  
  53. int String::len() const {
  54.   return STRLEN(getValue());
  55. }
  56.  
  57. int String::isempty() {
  58.   return (!val || !*val);
  59. }
  60.  
  61. void String::toupper() {
  62.   if (!isempty()) for (char *p = val; *p; p++) *p = TOUPPER(*p);
  63. }
  64.  
  65. void String::tolower() {
  66.   if (!isempty()) for (char *p = val; *p; p++) *p = TOLOWER(*p);
  67. }
  68.  
  69. int String::isequal(const char *otherval) const {
  70.   return !STRCMP(otherval, getValue());
  71. }
  72.  
  73. const char *String::printf(const char *format, ...) {
  74.   va_list args;
  75.   va_start (args, format);
  76.   vsprintf(format,args);
  77.   va_end(args);
  78.   return getValue();
  79. }
  80.  
  81.  
  82. void String::vsprintf(const char *format, va_list args) {
  83.   if(!format) return;
  84.   
  85.   va_list saveargs=args;
  86.   // roughly evaluate size of dest string
  87.   const char *p=format;
  88.   int length=0;
  89.   while(*p) {
  90.     if(*(p++)!='%') length++;
  91.     else {
  92.       void *arg=va_arg(args, void *);
  93.       for (;;) {
  94.         const char f=*p++;
  95.         if(f=='c') length++;
  96.         else if(f=='i') length+=16;
  97.         else if(f=='d') length+=32;
  98.         else if(f=='x') length+=32;
  99.         else if(f=='s') length+=STRLEN((const char *)arg);
  100.         else if(ISDIGIT(f)) continue;
  101.         else ASSERTPR(0,"undefined format passed to stringprintf!");
  102.         break;
  103.       }
  104.     }
  105.   }
  106.   if(val) {
  107.     if(len() < length) 
  108.       val=(char *)REALLOC(val,length+1);
  109.   } else val=(char *)MALLOC(length+1);
  110.  
  111.   // now write the string in val
  112.   int real_len = ::vsprintf(val,format,saveargs);
  113.   ASSERTPR(real_len <= length, "String.printf overflow");
  114.   // realloc to actual size used
  115.   if (real_len > 0 && (length - real_len) > wastage_allowed) {
  116.     val = (char *)REALLOC(val, real_len+1);
  117.   }
  118. }
  119.  
  120. const char *String::cat(const char *value) {
  121.   if (value == NULL || *value == 0) return getValue();
  122.   if (val == NULL) return setValue(value);
  123.   return setValue(StringPrintf("%s%s", getValue(), value));
  124. }
  125.  
  126. StringPrintf::StringPrintf(const char *format, ...)
  127.   : String(NULL) {
  128.   va_list args;
  129.   va_start (args, format);
  130.   vsprintf(format,args);
  131.   va_end(args);
  132. }
  133.  
  134. StringPrintf::StringPrintf(int val)
  135.   : String(NULL) {
  136.   *this += val;
  137. }
  138.  
  139. StringPrintf::StringPrintf(GUID g)
  140.   : String(NULL) {
  141.   char splab[nsGUID::GUID_STRLEN+1];
  142.   nsGUID::toChar(g, splab);
  143.   cat(splab);
  144. }
  145.  
  146. RecycleString::RecycleString(const char *initial_val) : String() {
  147.   setValue(initial_val);
  148. }
  149.  
  150. const char *RecycleString::reuseValue(const char *newval) {
  151.   // try to recycle the space
  152.   if (newval == val) return val;
  153.   if (newval != NULL && val != NULL && STRLEN(newval) <= len()) {
  154.     STRCPY(val, newval);
  155.   } else String::setValue(newval);    // just reset it
  156.   return val;
  157. }
  158.